package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.AccountCredDetails;
import com.csi.domain.AccountDetails;
import com.csi.domain.BindAccount;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@WebServlet("/user/queryBindAccountTypeController")
public class  QueryBindAccountTypeController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        // 获取请求参数
        String userNumber = req.getParameter("userNumber");

        UserService userService = new UserServiceImpl();

        try {
            // 查询用户已绑定卡的列表
            List<BindAccount> bindAccounts = userService.queryBindAccountList(userNumber);

            // 查询用户所有关联银行卡列表（储蓄卡和活期账户）
            List<AccountDetails> accountDetails = userService.queryAccountList(userNumber);

            // 查询用户所有关联银行卡列表（信用卡）
            List<AccountCredDetails> accountCredDetails = userService.queryCredAccountList(userNumber);

            // 获取已绑定卡的卡号列表
            List<String> bindAccountNumbers = new ArrayList<>();
            for (BindAccount bindAccount : bindAccounts) {
                bindAccountNumbers.add(bindAccount.getAccountNumber());
            }

            // 查询绑定的普通银行卡列表
            List<Object> bindAccountDetails = new ArrayList<>();
            for (AccountDetails accountDetail : accountDetails) {
                if (bindAccountNumbers.contains(accountDetail.getAccountNumber())) {
                    bindAccountDetails.add(accountDetail);
                }
            }

            // 查询绑定的信用卡列表
            List<Object> bindAccountCredDetails = new ArrayList<>();
            for (AccountCredDetails accountCredDetail : accountCredDetails) {
                if (bindAccountNumbers.contains(accountCredDetail.getAccountNumber())) {
                    bindAccountCredDetails.add(accountCredDetail);
                }
            }

            HashMap<String,Object> bindAccountMap = new HashMap<>();

            bindAccountMap.put("bindAccountDetails",bindAccountDetails);
            bindAccountMap.put("bindAccountCredDetails",bindAccountCredDetails);



            Result< HashMap<String,Object>> result = new Result<>();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setDatas(bindAccountMap);
            out.print(JSON.toJSONString(result));
        } catch (Exception e) {
            Result result = new Result<>();
            result.setCode(500);
            result.setMsg("服务器内部错误");
            out.print(JSON.toJSONString(result));
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
}

